/*
 * Eddited { 15/05/2012, 20:05 }
 * Clear all data, users inserted. 
 * Allow only add button to display. 
 */
function reset_data()
{
	document.getElementById('mat_id').value="";
	document.getElementById('name').value="";
	document.getElementById('price').value="";
	document.getElementById('category').selectedIndex=0;
	document.getElementById('add').style.display="block";
	document.getElementById('delete').style.display="none";
	document.getElementById('save').style.display="none";
	document.getElementById('close').style.display="block";
	document.getElementById('typeError').style.display="none";
	document.getElementById('nameError').style.display="none";
	document.getElementById('priceError').style.display="none";
	window.location.href = window.location.protocol + "//" + window.location.host + 
                       window.location.pathname + window.location.search + 
                       "#top";
}


/*
 * Eddited { 15/05/2012, 20:05 }
 * Delete and save buttons display. Disable add button.
 * The selected material's informations inserted into edit boxes. 
 */
function update_material( material_id, name, category, price )
{
	document.getElementById('add').style.display="none";
	document.getElementById('save').style.display="block";
	document.getElementById('delete').style.display="block";
	document.getElementById('close').style.display="block";
	
	document.getElementById('mat_id').value=material_id;
	document.getElementById('name').value=name;
	document.getElementById('price').value=price;
	
	switch(category)
	{
		case 'zyme':
			document.getElementById('category').selectedIndex=1;
			break;
		case 'sweet':
			document.getElementById('category').selectedIndex=2;
			break;
		case 'salty':
			document.getElementById('category').selectedIndex=3;
			break;
		default:
			document.getElementById('category').selectedIndex=0;
	}
	window.location.href = window.location.protocol + "//" + window.location.host + 
                       window.location.pathname + window.location.search + 
                       "#editBar";
}

/*
 * Eddited { 19/05/2012, 21:05 }
 * Adds a new material.
 */
function add_material()
{	
	document.getElementById('typeError').style.display="none";
	document.getElementById('nameError').style.display="none";
	document.getElementById('priceError').style.display="none";
	
	var name=document.getElementById('name').value;
	var price=document.getElementById('price').value;
	var category=document.getElementById('category').selectedIndex;
	var flag=1;
	
	switch(category)
	{	
		case 1:
			category == "zyme";
			break;
		case 2:
			category == "sweet";
			break;
		case 3:
			category == "salty";
			break;
		default:
			flag=0;
			//document.getElementById('typeError').style.display="inherit";
	}
	
	if(name=="")
	{
	flag=0;
	//document.getElementById('nameError').style.display="inherit";
	}
	
	if( !IsNumeric( price ) )
	{
	flag=0;
	//document.getElementById('priceError').style.display="inherit";
	}
	
	if(flag)
	{
		var url='../bus_functions/add_material.php?name='+name+'&type='+category+'&price='+price+'' ;
		location.href=url;
	}
}

/*
 * Eddited { 19/05/2012, 21:10 }
 * Deletes a material.
 */
function delete_material()
{
	var id=document.getElementById('mat_id').value;
	
	var url='../bus_functions/delete_material.php?id='+id+'' ;
	location.href=url;
}

/*
 * Eddited { 19/05/2012, 21:09 }
 * Saves an eddited material.
 */
function save_material()
{
	var name=document.getElementById('name').value;
	var price=document.getElementById('price').value;
	var category=document.getElementById('category').selectedIndex;
	var id=document.getElementById('mat_id').value;
	var flag=1;
	
	switch(category)
	{
		case 1:
			category == "zyme";
			break;
		case 2:
			category == "sweet";
			break;
		case 3:
			category == "salty";
			break;
		default:
			flag=0;
			document.getElementById('typeError').style.display="block";
	}
	
	if(name=="")
	{
	flag=0;
	document.getElementById('nameError').style.display="block";
	}
	
	if( !IsNumeric( price ) )
	{
	flag=0;
	document.getElementById('priceError').style.display="block";
	}
	
	if(flag)
	{
		var url='../bus_functions/save_material.php?name='+name+'&type='+category+'&price='+price+'&id='+id+'' ;
		location.href=url;
	}
}

function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}


/*
function showCustomer(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getcustomer.asp?q="+str,true);
xmlhttp.send();
}

*/